import axios from 'axios'
import qs from 'qs'
let baseUrl = 'http://192.168.2.27:3000'
//商品请求
export function request(config) {
    return axios({
        method: "GET",
        url: `${baseUrl}/posts?id=${config}`
    })
}

export function gethomesearch() {
    return axios({
        method: "GET",
        url: `${baseUrl}/homesearch`
    })
}

//用户请求
//用户注册判断
export function checkregister(id) {
    return axios({
        methods: "GET",
        url: `${baseUrl}/user?usernumber=${id}`
    })
}
//用户注册提交
export function registerpush(id, name, pass, realname, usersexy, userimg, usertitle, level, registertime, logintime, overduetime) {
    axios.post(`${baseUrl}/user`, {
        userid: id,
        usernumber: name,
        userpassword: pass,
        realname: realname,
        usersexy: usersexy,
        userimg: userimg,
        usertitle: usertitle,
        level: level,
        registertime: registertime,
        logintime: logintime,
        overduetime: overduetime
    })
}
//用户登录信息判断
export function checklogin(name, pass) {
    return axios.get(`${baseUrl}/user?usernumber=${name}&userpassword=${pass}`)
}
//获取用户信息
export function getuserinfo(token) {
    return axios.get(`${baseUrl}/user?userid=${token}`)
}

export function getuserimg(id) {
    return axios.get(`${baseUrl}/user?id=${id}`)
}
//提交用户意见
export function usermessagepush(name, email, message) {
    axios.post(`${baseUrl}/usermessage`, {
        username: name,
        useremail: email,
        usermessage: message
    })
}
//获取用户意见列表
export function getusermessage() {
    return axios.get(`${baseUrl}/usermessage`)
}
//获取用户长度
export function getuserlen() {
    return axios.get(`${baseUrl}/user`)
}
//获取登录过的用户列表
export function getloginuser() {
    return axios.get(`${baseUrl}/user?logintime!=''`)
}
//获取用户信息
export async function getuser(page = 1, limit = 5) {

    let instance = axios.get(`${baseUrl}/user?_page=${page}&_limit=${limit}`)
    return instance
}



//修改用户信息
export function changeuserinfo(id, userform) {
    return axios.patch(`${baseUrl}/user/${id}`, {
        usernumber: userform.formusernumber,
        usersexy: userform.formusersexy,
        realname: userform.formrealname,
        userimg: userform.formuserimg,
        usertitle: userform.formusertitle
    })

}
//修改用户VIP等级
export function changeuserlevel(id, level) {
    return axios.patch(`${baseUrl}/user/${id}`, {
        level: level
    })
}
//添加商品到购物车
export function pushcartlist(id,cart) {
    return axios.patch(`${baseUrl}/user/${id}`,{cartlist: cart})
}
export function getcartlist(token) {
    return axios.get(`${baseUrl}/user?userid=${token}`)
}
export function deleteuser(id) {
    return axios.delete(`${baseUrl}/user/${id}`)
}
//获取vip等级权限列表
export function getlevellist(id="") {
    if(id != "") {
        return axios.get(`${baseUrl}/levelcardlist?id=${id}`)
    }
    return axios.get(`${baseUrl}/levelcardlist`)
}
//修改用户密码
export function changeuserpass(id, pass) {
    return axios.patch(`${baseUrl}/user/${id}`, {
        userpassword: pass
    })
}
//修改用户登录时间
export function changelogintime(id, time) {
    return axios.patch(`${baseUrl}/user/${id}`, {
        logintime: time
    })
}
//修改用户会员过期时间
export function changeoverduetime(id, time='') {
    return axios.patch(`${baseUrl}/user/${id}`, {
        overduetime: time
    })
}

//新增兑换码
export function addcode(excode) {
    return axios.post(`${baseUrl}/exchangelist`, {
        code: excode.code,
        allnumber: excode.allnumber,
        surplusnumber: excode.surplusnumber,
        addtime: excode.addtime,
        exchangeday: excode.exchangeday,
        exchangeuser: []
    })
}
//获取所有兑换码
export function getallcode(code = '') {
    if(code == '') {
        return axios.get(`${baseUrl}/exchangelist`)
    }
    return axios.get(`${baseUrl}/exchangelist?code=${code}`)
}
//修改兑换码数据
export function changecode(id, sur,codelist) {
    // axios.post(`http://localhost:3000/exchangelist/${id}`, codelist)
    // console.log(codelist)
    return axios.patch(`${baseUrl}/exchangelist/${id}`, {
        surplusnumber: sur,
        exchangeuser: codelist
    })
}
//删除兑换码
export function deletecode(id) {
    return axios.delete(`${baseUrl}/exchangelist/${id}`)
}

//调用alipay接口
export function getalipay(testdata) {
    var instance = axios.create({headers: {'content-type': 'application/x-www-form-urlencoded'}});
             // 代理到  http://localhost:3000/api/pcpay
    return instance.post(`http://192.168.2.27:8085/pay/api/payment`, qs.stringify(testdata))
}

//上传修改年龄
export function test(id,test) {
    return axios.patch(`${baseUrl}/user/${id}`,qs.stringify(test))
}

// export function getimg(id) {
//     return axios.get(`http://192.168.2.27:3000/user/${id}`)
// }
//上传图片
export function patchimg(id,imgstr) {
    return axios.patch(`${baseUrl}/user/${id}`,qs.stringify(imgstr))
}

export function pushorder(orderinfo) {
    // console.log(orderinfo)
    return axios.post(`${baseUrl}/order`,orderinfo)
    // return new Promise()
    // return axios.post('http://192.168.2.27:3000/order',qs.stringify(orderinfo))
}
//获取订单信息
export function getorder(usernumber="",type='') {
    if(usernumber == '') {
        return axios.get(`${baseUrl}/order`)
    }

    if(type != '' && type != "不限") {
        return axios.get(`${baseUrl}/order?usernumber=${usernumber}&type=${type}`)
    }else if(type == '不限' || type == ''){
        return axios.get(`${baseUrl}/order?usernumber=${usernumber}`)
    }
    
}
export function changeorder(id,staus) {
    return axios.patch(`${baseUrl}/order/${id}`,{staus:staus})
}